const Multipart = require('../utils/Multipart.min.js')
var baseUrl='https://www.oyvvbdev.top/api';
async function get(url){
  var token=getApp().globalData.accessToken
    var result=await wx.p.request({
      url:baseUrl+url,
      method:'GET',
      header:{
          'content-type': 'application/json',
          'Authorization': 'Bearer '+token
      },
    })
    //请求模板
    if(result.statusCode==401){
      var user=getApp().globalData.user
      if(user!=null){
        wx.switchTab({
          url: '../calendar/calendar',
        })
      }else{
       await wx.p.navigateTo({
          url: '../fitin/fitin',
        })
      }
    }else{
      return result.data
    }
    
}
async function post(url,data){
  var token=getApp().globalData.accessToken
  var result= await wx.p.request({
    url:baseUrl+url,
    method:'POST',
    header:{
        'content-type': 'application/json',
        'Authorization': 'Bearer '+token
    },
    data:data
  })//请求模板
  console.log(result)
  if(result.statusCode==401){
    var user=getApp().globalData.user
    if(user!=null){
      wx.switchTab({
        url: '../calendar/calendar',
      })
    }else{
     await wx.p.navigateTo({
        url: '../fitin/fitin',
      })
    }
  }else{
    return result.data
  }

}
async function patch(url,data){
  console.log(baseUrl+url)
  var token=getApp().globalData.accessToken
  var result= await wx.p.request({
    url:baseUrl+url,
    method:'PATCH',
    header:{
        'content-type': 'application/json',
        'Authorization': 'Bearer '+token
    },
    data:data
  })//请求模板
  console.log(result)
  if(result.statusCode==401){
    var user=getApp().globalData.user
    if(user!=null){
      wx.switchTab({
        url: '../calendar/calendar',
      })
    }else{
     await wx.p.navigateTo({
        url: '../fitin/fitin',
      })
    }
  }else{
    return result.data
  }
}
async function del(url){
  var token=getApp().globalData.accessToken
    var result= await wx.p.request({
    url:baseUrl+url,
    method:'DELETE',
    header:{
        'content-type': 'application/json',
        'Authorization': 'Bearer '+token
    }
  })//请求模板
  if(result.statusCode==401){
    var user=getApp().globalData.user
    if(user!=null){
      wx.switchTab({
        url: '../calendar/calendar',
      })
    }else{
     await wx.p.navigateTo({
        url: '../fitin/fitin',
      })
    }
  }else{
    return result.data
  }
  
}
async function upload(url,fields,files){
  var token=getApp().globalData.accessToken
    console.log(files)
    console.log(fields)
  new Multipart({
    files,
    fields       
  }).submit(baseUrl+url,{'Authorization': 'Bearer '+token}).then(res=>{
    if(res.statusCode==401){
      var user=getApp().globalData.user
      if(user!=null){
        wx.switchTab({
          url: '../calendar/calendar',
        })
      }else{
        wx.p.navigateTo({
          url: '../fitin/fitin',
        })
      }
    }else{
      return res
    }
  })
  
}
async function uploadfile(url,formData,filePath,name){
  var token=getApp().globalData.accessToken
  wx.uploadFile({
    url:baseUrl+url,
    filePath:filePath,
    name:name,
    header:{
      'Authorization': 'Bearer '+token
    },
    formData:formData,
    success:async function(res){
      if(res.statusCode==401){
        var user=getApp().globalData.user
        if(user!=null){
          wx.switchTab({
            url: '../calendar/calendar',
          })
        }else{
         await wx.p.navigateTo({
            url: '../fitin/fitin',
          })
        }
      }else{
        return res
      }
    }
  })
  
}
module.exports={
  get:get,
  post:post,
  patch:patch,
  del:del,
  baseUrl:baseUrl,
  upload:upload,
  uploadfile:uploadfile
}